package com.urick.spi.dubbo.ext;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

/**
 * Design Theories:
 * <a href=""></a>
 *
 * @author uRick 2021/12/13 11:43
 * Email: rickwork@163.com
 * Wechat: yy3076815642
 * QQ: 3076815642
 * @since 1.0
 */
public class IExtResult {
    public final static int ok = 200;
    public final static int error = 999;
    private int code;
    private String msg;
    private String name;

    private IExtResult() {
    }

    public static IExtResult builder() {
        return new IExtResult();
    }

    public static IExtResult ok() {
        return builder().code(ok);
    }

    public static IExtResult error() {
        return builder().code(error);
    }

    public static IExtResult error(String msg) {
        return error().msg(msg);
    }

    public static boolean checkOk(IExtResult result) {
        if (ObjectUtil.isNull(result)) return false;
        return result.getCode() == ok;
    }

    public static boolean checkOkAndMsg(IExtResult result, String msg) {
        if (checkOk(result)) {
            return StrUtil.equals(result.getMsg(), msg);
        }
        return false;
    }

    public static boolean checkError(IExtResult result) {
        if (ObjectUtil.isNull(result)) return false;
        return result.getCode() == error;
    }

    public static boolean checkErrorAndMsg(IExtResult result, String msg) {
        if (checkError(result)) {
            return StrUtil.equals(result.getMsg(), msg);
        }
        return false;
    }

    public int getCode() {
        return code;
    }

    public IExtResult code(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public IExtResult msg(String msg) {
        this.msg = msg;
        return this;
    }

    public String getName() {
        return name;
    }

    public IExtResult name(String name) {
        this.name = name;
        return this;
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}
